package com.jian.sys.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import com.jian.sys.common.WebUtils;
import com.jian.sys.pojo.Notice;
import com.jian.sys.pojo.User;
import com.jian.sys.service.NoticeService;
import com.jian.sys.vo.NoticeVo;
import net.sf.jsqlparser.expression.DateValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 *  公告管理前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-09
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo){

        IPage<Notice> page = new Page<>(noticeVo.getPage(),noticeVo.getLimit());
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        wrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        wrapper.ge(noticeVo.getStartTime()!=null,"createtime",noticeVo.getStartTime());
        wrapper.le(noticeVo.getEndTime()!=null,"createtime",noticeVo.getEndTime());
        wrapper.orderByDesc("createtime");
        this.noticeService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加公告
     * @param noticeVo
     * @return
     */
    @RequestMapping("/addNotice")
    public ResultObj addNotice(NoticeVo noticeVo){
        User user = (User) WebUtils.getSession().getAttribute("user");
        noticeVo.setCreatetime(new Date());
        noticeVo.setOpername(user.getName());
        System.out.println(noticeVo);
        try{
            this.noticeService.save(noticeVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改公告
     * @param noticeVo
     * @return
     */
    @RequestMapping("/updateNotice")
    public ResultObj updateNotice(NoticeVo noticeVo){
//        User user = (User) WebUtils.getSession().getAttribute("user");
//        noticeVo.setCreatetime(new Date());
//        noticeVo.setOpername(user.getName());
        try{
            this.noticeService.updateById(noticeVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @RequestMapping("/deleteNotice")
    public ResultObj deleteNotice(Integer id){
        try {
            this.noticeService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @RequestMapping("/batchDeleteNotice")
    public ResultObj batchDeleteNotice(Integer[] ids){
        try {
            Collection<Serializable> serializables = new ArrayList<>(Arrays.asList(ids));
            this.noticeService.removeByIds(serializables);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("/loadAllNoticeToDeskManager")
    public DataGridView loadAllNoticeToDeskManager(){
        List<Notice> list = this.noticeService.list();
        for (Notice notice : list) {
            System.out.println(notice);
        }
        return new DataGridView(list);
    }
}

